class SceneEditor extends GuaScene {
    constructor(game) {
        super(game)
        this.setup()
    }

    setup() {
        this.context = this.game.context
        this.level = levels[1]
        this.plantCards = []
        this.plantchooseCards = []
        this.plantCard = []
        this.removeCard = false
        this.pawnMower = []
        this.offsetX = 255
        this.offsetY = 100
        this.zombieoffsetY = 20
        this.heightOfRow = 100
        this.cards = ['card_Peashooter', 'card_PotatoMine', 'card_Gralic', 'card_TwinSunflower', 'card_wallnut',
            'card_cherrybomb', 'card_chomper', 'card_hypnoshroom', 'card_iceshroom', 'card_jalapeno', 'card_repeaterpea',
            'card_snowpea', 'card_squash', 'card_sunflower', 'card_sunshroom', 'card_threepeashooter', 'card_PumpkinHead',
            'card_scaredyshroom', 'card_spikeweed'
        ]
        this.setupBG()
        this.setupLawnMower()
        this.setupCardBack()
        this.setupSeedChooserBack()
        this.setupInputs()

    }

    setupLawnMower() {
        for (let i = 1; i < 6; i++) {
            let lm = LawnMower.new(this.game)
            lm.y = this.zombieoffsetY + i * this.heightOfRow
            this.addElement(lm)
            this.pawnMower.push(lm)
        }

    }

    setupCardBack() {
        let b1 = PlantCard.new(this.game, 'card_back')
        let b2 = PlantCard.new(this.game, 'sun_back')
        let b3 = PlantCard.new(this.game, 'sun')
        let b4 = PlantCard.new(this.game, 'score_back')
        b1.x = 200
        b2.x = 210
        b3.x = 220
        b3.y = 10
        b4.x = 208
        b4.y = 60
        this.addElement(b1)
        this.addElement(b2)
        this.addElement(b3)
        this.addElement(b4)
    }

    update() {
        super.update();
    }

    draw() {
        super.draw()

    }

// setup 系列

    setupSeedChooserBack() {
        let b1 = PlantCard.new(this.game, 'seedChooser_Background')
        b1.x = 200
        b1.y = 100
        this.addElement(b1)
        for (let c of this.cards) {
            this.plantCard.push(c)
        }
        for (let i = 0; i < this.plantCard.length; i++) {
            this.addPlantchooseCard(this.plantCard[i], i)
        }
        this.addStartButton('start_leave')

    }

    addStartButton(name) {

        let start = PlantCard.new(this.game, name)
        start.x = 260
        start.y = 550
        this.addElement(start)
        this.start = start
    }

    setupBG() {
        let bg = GuaImage.new(this.game, 'bg1')
        this.addElement(bg)
    }


    setupInputs() {
        // mouse inputs
        let self = this
        let startDrag = false
        this.game.registerMouse(function (event, status) {
            let x = event.offsetX
            let y = event.offsetY
            let t = Math.floor(y / 50)
            let r = Math.floor((y - 100) / 70)
            let c = Math.floor(x / 50)
            if (t > 1) {
                for (let p of self.plantchooseCards) {
                    if (p.column === c && p.row === r) {
                        self.plantIcon = p
                    }
                }
            } else {
                for (let p of self.plantCards) {
                    if (p.column === c) {
                        self.removeCard = p
                    }
                }
            }


            if (status == 'down') {
                if (self.plantIcon) {
                    startDrag = true
                    self.icon = self.plantIcon.clone()
                    self.addElement(self.icon)
                    self.addPlantCard(self.icon.name, self.plantCards.length)
                    // self.plantCards.push(self.icon)
                }
                if (self.removeCard) {
                    self.removePlantCard(self.removeCard)
                    self.updatePlantCard()
                }
                // start.x = 260 305
                // start.y = 550 32
                if (x > 260 && x < 565 && y > 550 && y < 582) {
                    // log('点到了')
                    self.removeElement(self.start)
                    self.addStartButton('start_over')
                    // log('self.plantCards', self.plantCards)
                    self.updateScene()

                }
            } else {
                self.removeElement(self.icon)
                self.plantIcon = false
                startDrag = false
            }
        })

    }


// add 系列
    addPlantCard(name, column) {
        let p = PlantCard.new(this.game, name)
        p.x = 300 + column * 50
        p.y = 13
        p.column = Math.floor(p.x / 50)

        if (this.plantCards.length < 10) {
            this.addElement(p)
            this.plantCards.push(p)
        }
    }

    addPlantchooseCard(name, column) {
        let p = PlantCard.new(this.game, name)
        let c = parseInt(column / 8)
        let r = parseInt(column % 8)
        p.x = 210 + r * 50
        p.y = 110 + c * 70
        p.column = Math.floor(p.x / 50)
        p.row = Math.floor((p.y - 100) / 70)
        this.addElement(p)
        this.plantchooseCards.push(p)
    }

// remove 系列
    removePlantCard(card) {
        this.plantCards = this.plantCards.filter(e => {
            return e != card
        })
        this.removeElement(card)
        for (const x of this.plantCards) {
            this.removeElement(x)
        }

    }

// update 系列
    updatePlantCard() {

        for (let i = 0; i < this.plantCards.length; i++) {
            let p = this.plantCards[i]
            p.x = 300 + i * 50
            p.column = Math.floor(p.x / 50)

            this.addElement(p)
        }
    }

    updateScene() {
        for (const c of this.plantCards) {
            cards.push(c.name)
        }

        sleep(500).then(() => {

            let s = SceneTitle.new(this.game)
            this.game.replaceScene(s)
            // this.game.runWithScene(s)
        })
        this.game.removeMouse()
    }

}

const cards = []